package com.example.bmicalculator;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    private EditText etHeight;
    private EditText etWeight;
    private Button btnCalculate;
    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        etHeight = findViewById(R.id.etHeight);
        etWeight = findViewById(R.id.etWeight);
        btnCalculate = findViewById(R.id.btnCalculate);
        tvResult = findViewById(R.id.tvResult);


        btnCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculateBMI();
            }
        });
    }

    private void calculateBMI() {
        String heightStr = etHeight.getText().toString();
        String weightStr = etWeight.getText().toString();


        if (heightStr.isEmpty() || weightStr.isEmpty()) {
            tvResult.setText("请输入有效的身高和体重");
            return;
        }

        try {
            float height = Float.parseFloat(heightStr);
            float weight = Float.parseFloat(weightStr);

            if (height <= 0 || weight <= 0) {
                tvResult.setText("身高和体重必须大于0");
                return;
            }


            float bmi = weight / (height * height);
            String bmiFormatted = String.format("%.1f", bmi);
            String resultText = "你的BMI是: " + bmiFormatted + "\n" + getBMICategory(bmi);

            tvResult.setText(resultText);
        } catch (NumberFormatException e) {
            tvResult.setText("输入格式不正确");
        }
    }

    private String getBMICategory(float bmi) {
        if (bmi < 18.5) {
            return "体重过轻";
        } else if (bmi < 24.9) {
            return "健康体重";
        } else if (bmi < 29.9) {
            return "超重";
        } else {
            return "肥胖";
        }
    }
}